<?php
namespace app\admin\controller\account;

use app\admin\BaseController;
use app\admin\model\MoneyCard;
use app\lib\exception\FailedException;
use app\Validate\IdMustPositive;
use app\Validate\KeywordAddValidate;
use app\Validate\MoneyCardAddValidate;
use app\Validate\PageMustValidate;
use think\Request;


class Keyword extends BaseController
{
    protected $keyword ="关键字";
    /**
     * 查旬所有关键字
     * @param Request $request
     * @return \think\response\Json
     */
    public function keywordList(Request $request){
        (new PageMustValidate())->goCheck();
        $post = $request->post();
        $where = [];
        $where = addWhere($post,['keyword'=>''],$where,2);
        $count = (new \app\admin\model\Keyword())->getCount($where);
        $where['page'] = $post['page'];
        $list = (new \app\admin\model\Keyword())->wechatListWith("admin",$where,'*',"id desc",$post['page_number']);
        return writeJson(200,['count'=>$count,'data'=>$list]);
    }
    /**
     * 关键字添加
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function keywordAdd(Request $request){
        (new KeywordAddValidate())->goCheck();
        $post = $request->post();
        $new = [
            'keyword'=>$post['keyword'],
            'admin_id'=>$post['admin_id'],
        ];
        $res = (new \app\admin\model\Keyword())->dataUpdate($new);
        if(!$res){
            throw new FailedException(['msg'=>$this->keyword.'添加失败']);
        }
        return writeJson(200,[],$this->keyword.'添加成功');

    }
    /**
     * 会员充值编辑
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function keywordEdit(Request $request){
        (new IdMustPositive())->goCheck();
        (new KeywordAddValidate())->goCheck();
        $post = $request->post();
        $new = [
            'id'   =>$post['id'],
            'keyword'=>$post['keyword'],
            'admin_id'=>$post['admin_id'],
        ];
        $res = (new \app\admin\model\Keyword())->dataUpdate($new);
        if(!$res){
            throw new FailedException(['msg'=>$this->keyword.'编辑失败']);
        }
        return writeJson(200,[],$this->keyword.'编辑成功');
    }
    /**
     * 会员充值的删除
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function keywordDelete(Request $request){
        $post = $request->post();
        if(count($post['id'])==0){
            throw new FailedException(['msg'=>'请选择需要删除的资讯']);
        }
        $res = (new \app\admin\model\Keyword())->fieldUpdate('delete_time',time(),"",[['id','in',$post['id']]]);
        if(!$res){
            throw new FailedException(['msg'=>$this->keyword.'删除失败']);
        }
        return writeJson(200,[],$this->keyword.'删除成功');
    }

}
